﻿using System;
using System.Collections.Generic;
using System.Linq; 
using System.Web;
using System.Web.Mvc;

namespace IRIS.Models
{
    /// <summary>
    /// DataRepo is the connection between the Database and the controller. 
    /// </summary>
    public class DataRepo : IDataRepo
    {
        //Connection to the EntityModel
        IRISentityDataContext m_db = new IRISentityDataContext();

        #region Retrieve and Approve Article
        // collects all article that are dead but their CID is alive to retrieve
        public IEnumerable<Content> getDeadArticlesToRetrieve()
        {
            var result = from c in m_db.Contents
                         where c.Alive == false && c.ContentCategory.Alive == true && c.News == false && c.Modified > DateTime.Now.Date.AddDays(-30)
                         select c;
            return result;
        }
        //Gets an article that is not alive but the CID must be alive
        public Content getDeadArticleById(int id)
        {
            var result = (from c in m_db.Contents
                         where c.Alive == false && c.ContentCategory.Alive == true && c.News == false && c.ID == id
                         select c).SingleOrDefault();
            return result;
        }
        //Retrieve Article
        public void editDeadToAlive(Content theContent)
        {
            var cData = getDeadArticleById(theContent.ID);
            if (cData != null)
            {
                cData.Alive = true;
                cData.Approved = false;
                cData.Modified = DateTime.Now;
                m_db.SubmitChanges();
            }
        }
        //collects all Article that are not approved
        public IEnumerable<Content> getArticleToApprove()
        {
            var result = from c in m_db.Contents
                         where c.Alive == true && c.Approved == false && c.News == false
                         select c;
            return result;
        }
        public Content getArticleToApproveById(int id)
        {
            var result = (from c in m_db.Contents
                         where c.Alive == true && c.Approved == false && c.News == false && c.ID == id
                         select c).SingleOrDefault();
            return result;
        }

        #endregion
        #region Content - Article and News

        //Adds content (news or articles)
        public void addContent(Content theContent)
        {
            m_db.Contents.InsertOnSubmit(theContent);
            m_db.SubmitChanges();
        }
        
        
        //Takes in changes and edits the old data
        public void editContent(int id, Content theformData)
        {
            var nData = getContentById(id);
            if (nData != null)
            {
                if (theformData.Alive != false)
                {
                    nData.ContentText = theformData.ContentText;
                    nData.ContentTitle = theformData.ContentTitle;
                    nData.CID = theformData.CID;
                }
                nData.Modified = DateTime.Now;

                m_db.SubmitChanges();
            }
        }
        
        //Returns content News and articles
        public Content getContentById(int id)
        {
            var result = (from c in m_db.Contents
                          where c.Alive == true && c.ID == id
                          select c).SingleOrDefault();
            return result;
        }
        #endregion
        #region Articles
        //returns All approved Articles
        public IEnumerable<Content> getArticles()
        {
            var result = (from c in m_db.Contents
                          where c.Alive == true && c.Approved == true && c.News == false
                          orderby c.Created descending
                          select c);
            return result;
        } 
        public IEnumerable<Content> getArticleByCategoryId(int? cId)
        {
            var result = from c in m_db.Contents
                         where c.Alive == true && c.Approved == true && c.News == false && c.CID == cId 
                         select c;
            return result;
        }
        public IEnumerable<Content> getFiveNewestArticles()
        {
            var result = (from c in m_db.Contents
                          where c.Alive == true && c.Approved == true && c.News == false
                          orderby c.Created descending
                          select c).Take(5);
            return result;
        }
#endregion
        #region News
        public IEnumerable<Content> getNews()
        {
            var result = from c in m_db.Contents
                         where c.Alive == true && c.News == true
                         orderby c.Created descending
                         select c;
            return result;
        }
        #endregion
        #region ContentCategory
        public void addContentCategory(ContentCategory theCategory)
        {
            m_db.ContentCategories.InsertOnSubmit(theCategory);
            m_db.SubmitChanges();
        }
        public void editContentCategory(int id,ContentCategory theCategory)
        {
            var nData = getContentCategoryById(id);
            if (nData != null)
            {
                if (theCategory.Alive.Equals(true) )
                {
                    nData.Name = theCategory.Name;
                    nData.Description = theCategory.Description;
                }
                m_db.SubmitChanges();
            }
            
        }
        //Collects all categories exept News Category = id 8
        public IEnumerable<ContentCategory> getContentCategories()
        {
            var result = from c in m_db.ContentCategories
                         where c.Alive == true && c.ID != 8
                         select c;
            return result;
        }
        public ContentCategory getContentCategoryById(int? id)
        {
            var result = (from c in m_db.ContentCategories
                         where c.Alive == true && c.ID == id && c.ID != 8
                         select c).SingleOrDefault();
            return result;
        }
        #endregion
        #region Link
        public void saveLink(Link theLink)
        {
            m_db.Links.InsertOnSubmit(theLink);
            m_db.SubmitChanges();
        }
        //get Link By Id
        public Link getLinkById(int id)
        {
            var result = (from c in m_db.Links
                          where c.Alive == true && c.ID == id
                          select c).SingleOrDefault();
            return result;
        }
        public void updateLinkById(int id,Link link)
        {
            var lData = getLinkById(id);

            if (link.Alive == true)
            {
                lData.CID = link.CID;
                lData.Description = link.Description;
                lData.URL = link.URL;
                lData.Title = link.Title;
            }
                m_db.SubmitChanges();     
        }
        //Get All links
        public IEnumerable<Link> getLinks()
        {
            var result = from c in m_db.Links
                         where c.Alive == true
                         select c;
            return result;
        }
        #endregion
        #region LinkCategory
        public IEnumerable<LinkCategory> getLinkCategories()
        {
            var result = from c in m_db.LinkCategories
                         where c.Alive == true
                         select c;
            return result;
        }
        //Save LinkCategory to database
        public void addLinkCategory(LinkCategory linkCategory)
        {
            m_db.LinkCategories.InsertOnSubmit(linkCategory);
            m_db.SubmitChanges();

        }
        //Get LinkCATEGORY BY ID
        public LinkCategory getLinkCategoryById(int theId)
        {
            var result = (from c in m_db.LinkCategories
                          where c.Alive == true
                          where c.ID == theId
                          select c).SingleOrDefault();
            return result;
        }
        public void deleteLinkCategoryById(int theId)
        {
            LinkCategory linkCategory = getLinkCategoryById(theId);
            linkCategory.Alive = false;
        }
        public void updateLinkCategoryById(int theId,LinkCategory theData)
        {
            LinkCategory newLinkCategory = getLinkCategoryById(theId);

            if (theData.Alive != false)
            {
                newLinkCategory.Description = theData.Description;
                newLinkCategory.Name = theData.Name;
            }
            m_db.SubmitChanges();
        }
        public int getSizeOfAliveLinkCategories()
        {
            int size = (from c in m_db.LinkCategories
                        where c.Alive == true
                        select c).Count();
            return size;
        }
        #endregion
        #region Newsletter
        public IEnumerable<Newsletter> getNewsletters()
        {
            var result = from c in m_db.Newsletters
                       select c;
            return result;
        }

        public void addNewsletter(Newsletter theData)
        {

            m_db.Newsletters.InsertOnSubmit(theData);
            m_db.SubmitChanges();
        }

        public void deleteNewsletter(string theEmail)
        {

            var result = (from c in m_db.Newsletters
                          where c.Email == theEmail
                          select c).SingleOrDefault();
            m_db.Newsletters.DeleteOnSubmit(result);
            m_db.SubmitChanges();
                 
        }
        #endregion
        #region Sponsor
        public IEnumerable<Sponsor> getSponsors()
        {
            var result = from c in m_db.Sponsors
                         where c.Alive == true
                         select c;
            return result;
        }
        public Sponsor getSponsorById(int id)
        {
            var result = (from c in m_db.Sponsors
                        where c.ID == id 
                        select c).SingleOrDefault();
            return result;
        }
        public void addSponsor(Sponsor theFormData)
        {
            theFormData.Alive = true;
            m_db.Sponsors.InsertOnSubmit(theFormData);
            m_db.SubmitChanges();
        }
        public void editSponsor(int id, Sponsor theFormData)
        {
            Sponsor nData = getSponsorById(id);
            nData.Name = theFormData.Name;
            nData.Description = theFormData.Description;
            nData.URL = theFormData.URL;
            nData.Alive = theFormData.Alive;
            
            m_db.SubmitChanges();
        }
        #endregion
        #region EventCal
        public IEnumerable<EventCal> getEventCals()
        {
            var result = (from c in m_db.EventCals
                          where c.Alive == true
                          orderby c.EventDate descending 
                         select c);
            return result;
        }
        public EventCal getEventById(int? id)
        {
            var result = (from c in m_db.EventCals
                          where c.Alive == true && c.ID == id
                          select c).SingleOrDefault();
            return result;
        }
        public void addEvent(EventCal FormData)
        {
            m_db.EventCals.InsertOnSubmit(FormData);
            m_db.SubmitChanges();
        }
        public void editEvent(int id, EventCal formData)
        {
            var nData = getEventById(id);
            if (nData != null)
            {
                nData.Title = formData.Title;
                nData.Description = formData.Description;
                nData.EventDate = formData.EventDate;
                nData.EndDate = formData.EndDate;
                nData.Location = formData.Location;
                nData.lat = formData.lat;
                nData.lng = formData.lng;

                m_db.SubmitChanges();
            }
        }
        #endregion
        #region Staff
        public IEnumerable<Staff> getStaff()
        {
            var result = from c in m_db.Staffs
                         where c.Alive==true
                         select c;
            return result;
        }

        public void addStaff(Staff Formdata)
        {
            m_db.Staffs.InsertOnSubmit(Formdata);
            m_db.SubmitChanges();
        }
       
        public Staff getStaffById(int id)
        {
            var result = (from c in m_db.Staffs
                         where c.Alive == true && c.ID == id
                         select c).SingleOrDefault();
            return result;
        }

        
        public void editStaff(int id, Staff theformData)
        {
            var nData = getStaffById(id);
            if (nData != null)
            {
                if (theformData.Alive != false)
                {
                    nData.Name = theformData.Name;
                    nData.Phone = theformData.Phone;
                    nData.Position = theformData.Position;
                    nData.ID = theformData.ID;
                    nData.Image = theformData.Image;
                    nData.Email = theformData.Email;
                    nData.Education = theformData.Education;                  
                }
                m_db.SubmitChanges();
            }
        }
        #endregion
        #region FAQ
        public void addFAQ(FAQ theData)
        {
            m_db.FAQs.InsertOnSubmit(theData);
            m_db.SubmitChanges();
        }
        public void editFAQ(int id, FAQ theData)
        {
            var fData = getFAQById(id);
            if (fData != null)
            {
                fData.Title = theData.Title;
                fData.Question = theData.Question;
                fData.Answer = theData.Answer;

                m_db.SubmitChanges();
            }
        }
        public IEnumerable<FAQ> getFAQs()
        {
            var result = from c in m_db.FAQs
                         where c.Alive == true
                         select c;
            return result;
        }
        public FAQ getFAQById(int id)
        {
            var result = (from c in m_db.FAQs
                         where c.Alive == true && c.ID == id
                         select c).SingleOrDefault();
            return result;
        }
        #endregion
        #region Static
        public IEnumerable<StaticPage> getAllStaticPages()
        {
            var model = from c in m_db.StaticPages
                        where c.Alive == true && c.Id != 6 && c.Id != 7
                        select c;
            return model;
        }
        public void addStaticPage(StaticPage staticPage)
        {
            m_db.StaticPages.InsertOnSubmit(staticPage);
            m_db.SubmitChanges();
        }
        public StaticPage getStaticById(int id)
        {
            var model = (from c in m_db.StaticPages
                         where c.Id == id && c.Alive == true
                         select c).SingleOrDefault();
            
            return model;
        }

        public void deleteStaticPage(int id, StaticPage staticPage)
        {
            throw new NotImplementedException();
        }

        public void editStaticPage(int id, StaticPage theStaticPage)
        {
            var model = getStaticById(id);
            if (theStaticPage.Alive == true)
            {
                model.Text = theStaticPage.Text;
                model.Title = theStaticPage.Title;
            }
            theStaticPage.Modified = DateTime.Now;
            m_db.SubmitChanges();
        }

        #endregion
        #region Search
        public SearchViewModel search()
        {
            var result = new SearchViewModel
                             {
                                 m_Articles = getArticles(),
                                 m_Events = getEventCals(),
                                 m_News = getNews()
                             };
            return result;
        }
        #endregion
    }
}